package com.blb.controller;


import com.alibaba.fastjson.JSON;
import com.blb.entity.ProductionLine;
import com.blb.entity.Role;
import com.blb.entity.User;
import com.blb.service.IProductionLineService;

import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lifeng
 * @since 2019-11-15
 */

@RequestMapping("/productionLine")
@Controller
public class ProductionLineController {


    /**
     * production下的页面跳转
     * @param page
     * @return
     */
    @RequestMapping("{page}")
    public String roleManage(@PathVariable String page) {
        return "production/" + page;
    }

    @Autowired
    private IProductionLineService iProductionLineService;

    @ResponseBody
    @RequestMapping("getProductionName")
    public ProductionLine getProductionName(String productionName){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        ProductionLine productionName1 = iProductionLineService.getProductionName(productionName, companyId);
        return productionName1;
    }
    /**
     * 查看所有的生产线
     * @param model
     * @return
     */
    @GetMapping("allProductionLine")
    private String allProductionLine(Model model, HttpSession session) {
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<ProductionLine> productionLines = iProductionLineService.allProductionLine(companyId);
        model.addAttribute("productionLines", productionLines);
        return "production/sc_cn";
    }

    /**
     * 添加产线
     * @param productionLine
     * @return
     */
    @ResponseBody
    @PostMapping("addProductionLine")
    private String addProductionLine(@RequestBody ProductionLine productionLine, HttpSession session) {
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        productionLine.setCompanyId(companyId);
        //随机产线id
        int id = (int)((Math.random()*9+1)*100000);
        productionLine.setId(id);
        //判断此id在公司是否存在是否存在
        List<ProductionLine> id1 = iProductionLineService.findId(id, companyId);
        if(id1.equals(id)){
            int id2 = (int)((Math.random()*9+1)*100000);
            productionLine.setId(id2);
            iProductionLineService.save(productionLine);
        }else{
            iProductionLineService.save(productionLine);
        }
        return null;
    }
    /**
     * 删除产线
     * @param id
     * @return
     */
    @PostMapping("delProductionLine")
    private String delProductionLine(@RequestBody String id, HttpSession session) {
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        String i = id.substring(3, id.length());
        iProductionLineService.remove(i,companyId);
        return "redirect:/productionLine/allProductionLine";
    }

    /**
     * 修改产线
     * @param productionLine
     * @return
     */
    @ResponseBody
    @PostMapping("updProductionLine")
    private void updProductionLine(@RequestBody ProductionLine productionLine,HttpSession session) {
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        productionLine.setCompanyId(companyId);
        System.out.println("修改" + productionLine);
        iProductionLineService.updProductionLineMapper(productionLine);
    }
}

